package com.wellsoft.mobileoa.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wellsoft.mobileoa.LoginActivity;
import com.wellsoft.mobileoa.dbmodel.ContactInfo;
import com.wellsoft.mobileoa.utils.view.JGGView;
import com.wellsoft.mobileoa.utils.view.PostFailedView;
import com.wellsoft.mobileoa.wsmodel.WSOA_Dept;
import com.wellsoft.mobileoa.wsmodel.WSOA_DownloadUnit_Request;
import com.wellsoft.mobileoa.wsmodel.WSOA_DownloadUnit_Response;
import com.wellsoft.mobileoa.wsmodel.WSOA_Page;
import com.wmobile.utils.BaseObject;
import com.wmobile.utils.DataHelper;
import com.wmobile.utils.PopMessageDialog;
import com.wmobile.utils.ServiceResponse;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;

public class BaseActivity extends Activity {
	protected DataHelper dataHelper;
	protected String url;
	protected WSOA_Page page;
	protected BaseView baseView;
	protected ViewGroup viewGroup;
	protected AlertDialog defaultDialog = null;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public WSOA_Page getPage() {
		return page;
	}

	public void setPage(WSOA_Page page) {
		this.page = page;
	}

	public BaseView getBaseView() {
		return baseView;
	}

	public void setBaseView(BaseView baseView) {
		this.baseView = baseView;
	}

	public ViewGroup getViewGroup() {
		return viewGroup;
	}

	public void setViewGroup(ViewGroup viewGroup) {
		this.viewGroup = viewGroup;
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemConfig.pushActivity(this);
	}

	public void NewActivity(Intent intent) {
		NewActivity(intent, false, 0);
	}

	public void NewActivity(Intent intent, boolean isCallForResult,
			int requestCode) {
		if (isCallForResult) {
			startActivityForResult(intent, requestCode);
		} else {
			startActivity(intent);
		}
	}

	/**
	 * 返回到前一个页面
	 */
	public void returnActivity() {
		closeWaitingDialog();
		SystemConfig.popActivity();
		this.finish();
	}

	/**
	 * 返回到主页面
	 */
	public void returnHomeActivity() {
		SystemConfig.setGoBackUrl(null);
		BaseActivity baseActivity = SystemConfig.getCurrentActivity();
		while (!baseActivity.getBaseView().getClass().getSimpleName()
				.equals(JGGView.class.getSimpleName())) {
			baseActivity.returnActivity();
			baseActivity = SystemConfig.getCurrentActivity();
		}
	}

	/**
	 * 返回到登陆页面
	 */
	public static void returnLoginActivity() {
		BaseActivity baseActivity = SystemConfig.getCurrentActivity();
		while (!baseActivity.getClass().getSimpleName()
				.equals(LoginActivity.class.getSimpleName())) {
			baseActivity.returnActivity();
			baseActivity = SystemConfig.getCurrentActivity();
		}
	}

	public AlertDialog showWaitingDialog() {
		return showAlertDialog(this, "处理中，请稍候...", null, null, null);
	}

	public void closeWaitingDialog() {
		if (defaultDialog != null && defaultDialog.isShowing()) {
			defaultDialog.dismiss();
		}
	}

	public void reloadView(WSOA_Page page, String url) {
		this.page = page;
		this.url = url;
		this.baseView = BaseView.CreateBaseViewFactory(this, page, url);
		// 使用BaseView子类将要显示的View内容生成，使用DrawSelf方法
		viewGroup = baseView.DrawSelf();
		if (viewGroup == null) {
			return;
		}
		setContentView(viewGroup);
	}

	public static void returnGobackActivity(String targetUrl) {
		returnGobackActivity(targetUrl, null, null);
	}

	public static void returnGobackActivity(String targetUrl,
			WSOA_Page newPage, String newUrl) {
		BaseActivity tempActivity;
		while (true) {
			tempActivity = SystemConfig.getCurrentActivity();
			if (tempActivity == null || tempActivity.url == null)
				break;
			if (tempActivity.url.equals(targetUrl)) {
				if (newPage != null) {
					tempActivity.reloadView(newPage, newUrl);
				}
				break;
			} else {
				tempActivity.returnActivity();
			}
		}
	}

	/**
	 * 提示信息
	 * 
	 * @param message
	 */
	public void showMessage(String message) {
		if (SystemConfig.stringIsNullOrEmpty(message))
			return;
		PopMessageDialog dialog = new PopMessageDialog(this, message);

		dialog.showDialog();
	}

	protected OnClickListener returnButtonListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			BaseActivity.this.returnActivity();
		}

	};

	/**
	 * 弹出对话框(重载) 未传入View参数，用于是/否框
	 */
	protected AlertDialog showAlertDialog(Context context, String title,
			DialogInterface.OnClickListener onConfirmClickListener,
			DialogInterface.OnClickListener onCancelClickListener) {
		return showAlertDialog(context, title, null, onConfirmClickListener,
				onCancelClickListener);
	}

	/**
	 * 弹出对话框(重载) 未传入negativebuttonlistener参数，用于信息框
	 */
	protected AlertDialog showAlertDialog(Context context, String title,
			DialogInterface.OnClickListener onConfirmClickListener) {
		return showAlertDialog(context, title, null, onConfirmClickListener,
				null);
	}

	/**
	 * 弹出对话框
	 */
	public AlertDialog showAlertDialog(Context context, String title,
			View view, DialogInterface.OnClickListener onConfirmClickListener,
			DialogInterface.OnClickListener onCancelClickListener) {
		if (defaultDialog != null && defaultDialog.isShowing()) {
			return defaultDialog;
		}
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
		dialogBuilder.setTitle(title);
		if (view != null) {
			dialogBuilder.setView(view);
		}
		if (onConfirmClickListener != null) {
			dialogBuilder.setPositiveButton("提交", onConfirmClickListener);
		}
		if (onCancelClickListener != null) {
			dialogBuilder.setNegativeButton("取消", onCancelClickListener);
		}
		AlertDialog alertDialog = dialogBuilder.create();
		defaultDialog = alertDialog;
		alertDialog.setCanceledOnTouchOutside(false);
		alertDialog.show();
		return alertDialog;
	}

	/**
	 * 关闭对话框
	 */
	protected DialogInterface.OnClickListener closeAlertDialogListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
		}

	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}
}